package com.ruoyi.framework.aspectj;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampSrLog;
import com.ruoyi.eamp.domain.vo.EampReManVO;
import com.ruoyi.eamp.service.IEampSrLogService;
import com.ruoyi.eamp.service.IEampSrManService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.time.LocalDateTime;


@Aspect
@Component
public class EampReManUpdateAspect {

    @Autowired
    private IEampSrManService eampSrManService;

    @Autowired
    private IEampSrLogService eampSrLogService;

    @Pointcut("@annotation(com.ruoyi.common.annotation.IEampReEditLog)")
    public void updateEampReManPointcut() {
    }

    @Around("updateEampReManPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 当前登录用户
        Long userId = SecurityUtils.getUserId();
        // 操作时间
        LocalDateTime localDate = LocalDateTime.now();
        // 操作方法
        String menthodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Long modifiedWarehouse = 0L;
        Long originalWarehouse = 0L;
        Long reId = 0L;
        for (Object arg : args) {
            if(arg instanceof EampReManVO){
                modifiedWarehouse = ((EampReManVO) arg).getModifiedWarehouse();
                originalWarehouse = ((EampReManVO) arg).getOriginalWarehouse();
                reId = ((EampReManVO) arg).getEampReMan().getReId();
            }
        }
        Object result = joinPoint.proceed();

        if(originalWarehouse != null && modifiedWarehouse != originalWarehouse){
            eampSrManService.updateAddEampSrManById(modifiedWarehouse);
            eampSrManService.updateRemoveEampSrManById(originalWarehouse);
            /*
             * 在库房记录增加
             * */
            EampSrLog eampSrLog = new EampSrLog(null,userId,localDate,menthodName,originalWarehouse,reId,1L);
            eampSrLogService.insertEampSrLog(eampSrLog);
        }

        return result;

    }
}
